<?php
class ReconlistApp extends MemberbaseApp
{
	var $_recon_mod;
	
	function __construct(){
		parent::__construct();
		$this->ReconlistApp();
	}
	
	function ReconlistApp(){
		$this->_recon_mod=&m('reconlist');
	}
	
	
	//对账清单
	function index(){
		$user=$this->visitor->get();
		$file_mod=&m('upload');
		$conditions=$this->_get_query_conditions(array(
			array(
				'field' => 'order_release_start',
				'equal' => '>=',
				'name'	 => 'release_date',
				'handler'=> 'gmstr2time',
			),
			array(
				'field' => 'order_release_end',
				'equal' => '<=',
				'name'	 => 'release_date2',
				'handler'=> 'gmstr2time_end',
			)
		));
		if ($user['role']==USER_FACTORY){
			$conditions.=" and recon.factory_id=".$user['factory_id'];
		}elseif ($user['role']==USER_FINANCE){
			
		}else{
			$this->show_warning("no_powers_to_visit");
			return;
		}
		$page = $this->_get_page();
		$reconlist = $this->_recon_mod->find(array(
				'join'=>'belongs_to_factory',
				'conditions'=>'1=1 '.$conditions,
				'order' => 'order_release_start ASC,order_release_end asc',
				'count' => true,
				'limit' => $page['limit']
		));
		if (!empty($reconlist)){
			foreach ($reconlist as $key=>$val){
				$recon_id=$val['recon_id'];
				$reconlist[$key]['recon_file']=$file_mod->find(array(
					'conditions'=>'file_type='.BELONGS_STATEMENT_FILE.' and key_id='.$recon_id,
					'order'=>'file_id desc'	
				));
			}
		}
		$page['item_count'] = $this->_recon_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('reconlist', $reconlist);
		$this->assign('belongs_state_file',BELONGS_STATEMENT_FILE);
		$this->assign('user',$user);
		$this->display('order.moneylist.html');
		$this->_curlocal(Lang::get('reconlist'));
	}
	
	function add_reconlist(){
		$user=$this->visitor->get();
		if ($user['role']!=USER_FACTORY){
			$this->show_warning("no_powers_to_visit");
			return;
		}
		$order_mod=&m('order');
		$member_mod=&m('member');
		$sale_return_mod=&m('order_salesreturn');
		if (!IS_POST){
			$order_arr_id=array();
			$order_arr=0;
			$order_release_start=isset($_GET['release_date_moneylist'])?$_GET['release_date_moneylist']:0;
			$order_release_end=isset($_GET['release_date_moneylist2'])?$_GET['release_date_moneylist2']:0;
			$start=strtotime($order_release_start);
			$end=strtotime($order_release_end);
			$start_week=date('D',$start);
			$end_week=date('D',$end);
			$result_date=($end-$start)/(60*60*24);
			if ($start_week!='Mon'){
				$this->show_warning("开始入仓期必须为星期一!");
				return;
			}
			if ($end_week!='Sun'){
				$this->show_warning("截止入仓期必须为星期日!");
				return;
			}
			if ($result_date!=6){
				$this->show_warning("入仓期只限一周");
				return;
			}
			$order_list=$order_mod->find(array(
				'conditions'=>'status='.ORDER_STATUS_SHIPPED.' and release_date>='.strtotime($order_release_start).' and release_date<='.strtotime($order_release_end).' and factory_id='.$user['factory_id'],
				'order'=>'release_date desc'	
			));
			if (!empty($order_list)){
				foreach ($order_list as $rows){
					$order_arr_id[]=$rows['order_id'];
				}
			}
			$amount_recon=$order_mod->get(array(
					'conditions'=>'status='.ORDER_STATUS_SHIPPED.' and release_date>='.strtotime($order_release_start).' and release_date<='.strtotime($order_release_end).' and factory_id='.$user['factory_id'],
					'fields'=>'sum(ship_amount) as total_ship',
					'order'=>'release_date desc'
			));
			if (!empty($order_arr_id)){
				$order_arr=implode(',', $order_arr_id);
			}
			$factory_info=$member_mod->get(array(
					'join'=>'has_factory',
					'conditions'=>'member.user_id='.$user['user_id'],
			));
			$sale_return_list=$sale_return_mod->find(array(
				'join'=>'belongs_order',
				'conditions'=>'re_status<3 and factory_id='.$user['factory_id']	
			));
			$sale_amount=$sale_return_mod->get(array(
					'join'=>'belongs_order',
					'conditions'=>'re_status<3 and factory_id='.$user['factory_id'],
					'fields'=>'sum(re_money) as recon_return_price'
			));
			$recon_info=$this->_recon_mod->get(array(
					'conditions'=>'order_release_start>='.strtotime($order_release_start).' and order_release_end<='.strtotime($order_release_end).' and factory_id='.$user['factory_id']
			));
			$other_amount=0;
			if (!empty($recon_info)){
				$other_amount=$recon_info['other_price'];
				$this->assign('recons',$recon_info);
			}
			$last_amount=$amount_recon['total_ship']-$sale_amount['recon_return_price']-$other_amount;
			$this->assign('order_return',$sale_return_list);
			$this->assign('ret_total',$sale_amount);
			$this->assign('factory_info',$factory_info);
			$this->assign('total',$amount_recon);
			$this->assign('recon_start',$order_release_start);
			$this->assign('recon_end',$order_release_end);
			$this->assign('order_arr',$order_arr);
			$this->assign('order_list',$order_list);
			$this->assign('last_amount',$last_amount);
			$this->assign('user',$user);
			$this->display('order.recon_detail.html');
			$this->_curlocal(Lang::get('reconlist'));
		}else{
			$data=array();
			$recon_start=$_POST['order_release_start'];
			$recon_end=$_POST['order_release_end'];
			$order_arr=$_POST['order_arr'];
			if (!$recon_start || !$recon_end){
				$this->show_warning("入仓期必须有开始时间跟截止时间");
				return;
			}
			if (!$order_arr){
				$this->show_warning("没有清单列表可生成");
				return;
			}
			$data['order_release_start']=strtotime($recon_start);
			$data['order_release_end']=strtotime($recon_end);
			$data['factory_id']=$user['factory_id'];
			$data['recon_time']=gmtime();
			$data['recon_price']=$_POST['total_ship'];
			$data['recon_return_price']=$_POST['recon_return_price'];
			$data['recon_fact_price']=$_POST['last_amount'];
			$recon_info=$this->_recon_mod->get(array(
				'conditions'=>'order_release_start>='.$data['order_release_start'].' and order_release_end<='.$data['order_release_end'].' and factory_id='.$user['factory_id']	
			));
			if (!empty($recon_info)){
				$recon_id=$recon_info['recon_id'];
				$this->_recon_mod->edit($recon_id,$data);
			}else{
				$recon_id=$this->_recon_mod->add($data);
			}
			if ($this->_recon_mod->has_error()){
				$this->show_warning($this->_recon_mod->get_error());
				return;
			}else{
				$order_arr_id=explode(',', $order_arr);
				$order_mod->edit('order_id'.db_create_in($order_arr_id),array('recon_id'=>$recon_id));
				if ($order_mod->has_error()){
					$this->show_warning($order_mod->get_error());
					return;
				}
			}
			$this->show_message('add_recon_success',Lang::get('reconlist'),'index.php?app=reconlist');
		}
	}
	
	
	//查看
	function view(){
		$recon_id=isset($_GET['recon_id'])?$_GET['recon_id']:0;
		$user=$this->visitor->get();
		if (!in_array($user['role'], array(USER_FACTORY,USER_FINANCE))){
			$this->show_warning("no_powers_to_visit");
			return;
		}
		$recon_info=$this->_recon_mod->get($recon_id);
		if (empty($recon_info)){
			$this->show_warning("该对账单不存在");
			return;
		}
		$order_mod=&m('order');
		$member_mod=&m('member');
		$sale_return_mod=&m('order_salesreturn');
		if (!IS_POST){
			$return_arr=array();
			$order_list=$order_mod->find(array(
					'conditions'=>'recon_id='.$recon_id,
					'order'=>'release_date desc'
			));
			$amount_recon=$order_mod->get(array(
					'conditions'=>'recon_id='.$recon_id,
					'fields'=>'sum(ship_amount) as total_ship',
					'order'=>'release_date desc'
			));
			$factory_info=$member_mod->get(array(
					'join'=>'has_factory',
					'conditions'=>'factory_info.factory_id='.$recon_info['factory_id'],
			));
			if ($recon_info['recon_status']==1){
				$sale_return_list=$sale_return_mod->find(array(
						'join'=>'belongs_order',
						'conditions'=>'order_sales_return.re_id='.$recon_info['recon_id'].' and factory_id='.$recon_info['factory_id']
				));
				$sale_amount=$sale_return_mod->get(array(
						'join'=>'belongs_order',
						'conditions'=>'order_sales_return.re_id='.$recon_info['recon_id'].' and factory_id='.$recon_info['factory_id'],
						'fields'=>'sum(re_money) as recon_return_price'
				));
			}else{
				$sale_return_list=$sale_return_mod->find(array(
						'join'=>'belongs_order',
						'conditions'=>'re_status<3 and factory_id='.$recon_info['factory_id']
				));
				$sale_amount=$sale_return_mod->get(array(
						'join'=>'belongs_order',
						'conditions'=>'re_status<3 and factory_id='.$recon_info['factory_id'],
						'fields'=>'sum(re_money) as recon_return_price'
				));
			}
			if (!empty($sale_return_list)){
				foreach ($sale_return_list as $v){
					$return_arr[]=$v['rid'];
				}
				$return_arr_id=implode(',', $return_arr);
				$this->assign('return_arr_id',$return_arr_id);
			}
			$other_amount=0;
			if (!empty($recon_info)){
				$other_amount=$recon_info['other_price'];
			}
			$last_amount=$amount_recon['total_ship']-$sale_amount['recon_return_price']-$other_amount;
			$this->assign('order_return',$sale_return_list);
			$this->assign('ret_total',$sale_amount);
			$this->assign('factory_info',$factory_info);
			$this->assign('total',$amount_recon);
			$this->assign('order_list',$order_list);
			$this->assign('recons',$recon_info);
			$this->assign('user',$user);
			$this->assign('last_amount',$last_amount);
			$this->display('reconlist.view.html');
			$this->_curlocal(Lang::get('view_reconlist'));
		}else{
			$data=array();
			$return_id=0;
			$data['recon_return_price']=$_POST['recon_return_price'];
			$data['recon_demo']=$_POST['recon_demo'];
			$data['other_price']=$_POST['other_price'];
			$data['recon_price']=$_POST['total_ship'];
			$data['recon_fact_price']=$_POST['total_ship']-$_POST['other_price']-$_POST['recon_return_price'];
			$btn_sub=$_POST['sub_recon'];
			if ($user['role']==USER_FINANCE){
				if ($btn_sub=="已付款"){
					$data['recon_status']=1;
					$return_id=isset($_POST['return_arr'])?$_POST['return_arr']:"";
				}
				$this->_recon_mod->edit($recon_id,$data);
			}
			if ($this->_recon_mod->has_error()){
				$this->show_warning($this->_recon_mod->get_error());
				return;
			}
			if ($return_id){
				$ret_arr_id=explode(',', $return_id);
				$sale_return_mod->edit('rid'.db_create_in($ret_arr_id),array('re_id'=>$recon_id,'re_status'=>SALES_RETURN_STATUS_DEDUCT));
				if ($sale_return_mod->has_error()){
					$this->show_warning($sale_return_mod->get_error());
					return;
				}
			}
			$this->show_message('sub_recon_success');
		}
	}
	
	
	//打印对账单
	function print_recon(){
	$recon_id=isset($_GET['recon_id'])?$_GET['recon_id']:0;
		$user=$this->visitor->get();
		if (!in_array($user['role'], array(USER_FACTORY,USER_FINANCE))){
			$this->show_warning("no_powers_to_visit");
			return;
		}
		$recon_info=$this->_recon_mod->get($recon_id);
		if (empty($recon_info)){
			$this->show_warning("该对账单不存在");
			return;
		}
		$order_mod=&m('order');
		$member_mod=&m('member');
		$sale_return_mod=&m('order_salesreturn');
		if (!IS_POST){
			$return_arr=array();
			$order_list=$order_mod->find(array(
					'conditions'=>'recon_id='.$recon_id,
					'order'=>'release_date desc'
			));
			$amount_recon=$order_mod->get(array(
					'conditions'=>'recon_id='.$recon_id,
					'fields'=>'sum(ship_amount) as total_ship',
					'order'=>'release_date desc'
			));
			$factory_info=$member_mod->get(array(
					'join'=>'has_factory',
					'conditions'=>'factory_info.factory_id='.$recon_info['factory_id'],
			));
			if ($recon_info['recon_status']==1){
				$sale_return_list=$sale_return_mod->find(array(
						'join'=>'belongs_order',
						'conditions'=>'order_sales_return.re_id='.$recon_info['recon_id'].' and factory_id='.$recon_info['factory_id']
				));
				$sale_amount=$sale_return_mod->get(array(
						'join'=>'belongs_order',
						'conditions'=>'order_sales_return.re_id='.$recon_info['recon_id'].' and factory_id='.$recon_info['factory_id'],
						'fields'=>'sum(re_money) as recon_return_price'
				));
			}else{
				$sale_return_list=$sale_return_mod->find(array(
						'join'=>'belongs_order',
						'conditions'=>'re_status<3 and factory_id='.$recon_info['factory_id']
				));
				$sale_amount=$sale_return_mod->get(array(
						'join'=>'belongs_order',
						'conditions'=>'re_status<3 and factory_id='.$recon_info['factory_id'],
						'fields'=>'sum(re_money) as recon_return_price'
				));
			}
			if (!empty($sale_return_list)){
				foreach ($sale_return_list as $v){
					$return_arr[]=$v['rid'];
				}
				$return_arr_id=implode(',', $return_arr);
				$this->assign('return_arr_id',$return_arr_id);
			}
			$other_amount=0;
			if (!empty($recon_info)){
				$other_amount=$recon_info['other_price'];
			}
			$last_amount=$amount_recon['total_ship']-$sale_amount['recon_return_price']-$other_amount;
			$this->assign('order_return',$sale_return_list);
			$this->assign('ret_total',$sale_amount);
			$this->assign('factory_info',$factory_info);
			$this->assign('total',$amount_recon);
			$this->assign('order_list',$order_list);
			$this->assign('recons',$recon_info);
			$this->assign('user',$user);
			$this->assign('last_amount',$last_amount);
			$this->display('reconlist.view.html');
			$this->_curlocal(Lang::get('view_reconlist'));
		}
	}
}